package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.RepertoryMapper;
import com.jt.pojo.Item;
import com.jt.pojo.Repertory;
import com.jt.pojo.Staff;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class RepertoryServiceImpl implements RepertoryService{
    @Autowired
    private RepertoryMapper repertoryMapper;



    @Transactional
    @Override
    public void updateStatus(Repertory repertory) {
        repertoryMapper.updateById(repertory);
    }

    @Transactional
    @Override
    public void deleteItemckById(Integer id) {
        repertoryMapper.deleteItemckById(id);
    }

    @Override
    public Repertory getItemckById(Integer id) {
        return repertoryMapper.selectById(id);
    }
    @Transactional
    @Override
    public void updateItemck(Repertory repertory) {
        repertoryMapper.updateById(repertory);
    }

    @Override
    public void additemck(Repertory repertory) {
        repertory.setStatus(true).setCreated(new Date());
        repertoryMapper.insert(repertory);
    }

    @Override
    public PageResult getItemckList(PageResult pageResult) {
        //1.总数
        //2.分页结果
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Repertory> queryWrapper =new QueryWrapper<>();
        queryWrapper.like(flag,"gname",pageResult.getQuery());

        //编辑mp的分页对象 四个属性有用(页数/条数/总数/记录) 传递= 页面/条数
        IPage<Repertory> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page = repertoryMapper.selectPage(page, queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Repertory> rows = page.getRecords();
        //将数据封装
        return pageResult.setTotal(total).setRows(rows);
    }




}
